﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.Contract;
using DevExpress.XtraBars;

namespace EFong.View.Customer
{
    public partial class Frm_CustomerEdit : BasicForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}

        public Frm_CustomerEdit()
        {
            InitializeComponent();
            
        }

        protected override void InitFields()
        {
            spe_CFlightIntergralRate.Properties.DisplayFormat.FormatString = "#,0.####";
            spe_CFlightIntergralRate.Properties.EditFormat.FormatString = "#,0.####";
            spe_HotelIntergralRate.Properties.DisplayFormat.FormatString = "#,0.####";
            spe_HotelIntergralRate.Properties.EditFormat.FormatString = "#,0.####";
        }

        protected override void Events()
        {
            this.gv_Passengers.FocusedRowChanged += (s, e) =>
                {
                    SelPass = this.gv_Passengers.GetRow(e.FocusedRowHandle) as Passenger;
                };
            this.gv_Passengers.CustomDrawCell += (s, e) =>
                {
                    this.gv_Passengers.CustomDrawCell<Passenger>(e,
                        (pass) =>
                        {
                            return !pass.Effective;
                        },
                        () =>
                        {
                            e.Appearance.ForeColor = Color.Red;
                        });

                    if (e.Column == colTel)
                    {
                        Passenger passenger = this.gv_Passengers.GetRow(e.RowHandle) as Passenger;

                        if (passenger == null)
                            return;

                        if (!string.IsNullOrEmpty(passenger.Tel1) && !string.IsNullOrEmpty(passenger.Tel2))
                            e.DisplayText = string.Format("{0} ; {1}", passenger.Tel1, passenger.Tel2);
                        else
                            e.DisplayText = string.IsNullOrEmpty(passenger.Tel1) ? passenger.Tel2 : passenger.Tel1;
                    }
                };

            this.btn_SetWebUser.Click += (s, e) =>
                {
                    this.setWebUser();
                };
        }

        protected override void LoadDatas()
        {
            bds_CustomerHotelIntergralRate.DataSource = _baseProxy.GetOriginalHotelIntergralRateList().ReturnValue;
            cmb_Salesman.Properties.BindingData(_baseProxy.GetEffUser().ReturnValue);
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();
                    break;
                case "bbt_Edit":
                    this.edit();
                    break;
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_PassengerEffective":
                    this.passengerEffective();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void passengerEffective()
        {
            if (SelPass == null)
                return;

            SelPass.Effective = false;

            this.gv_Passengers.BeginDataUpdate();
            this.gv_Passengers.EndDataUpdate();
        }

        private void add()
        {
            Frm_PassengerEdit frm = new Frm_PassengerEdit();
            frm.Text = "新增乘客";
            frm.Load += (s, e) =>
                {
                    frm.CurPass = new Passenger();
                };

            DialogResult addPassengerResult = FormOpener.ShowDialog(frm);

            if (addPassengerResult == DialogResult.OK)
            {
                this.gv_Passengers.BeginDataUpdate();
                ((List<Passenger>)this.bds_Passenger.DataSource).Add(frm.CurPass);
                this.gv_Passengers.EndDataUpdate();
            }
        }

        private void edit()
        {
            Frm_PassengerEdit frm = new Frm_PassengerEdit();
            frm.Text = "修改乘客资料";
            frm.Load += (s, e) =>
                {
                    frm.CurPass = SelPass;
                };

            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
            {
                this.gv_Passengers.BeginDataUpdate();
                this.gv_Passengers.EndDataUpdate();
            }
        }

        private void save()
        {
            this.gv_HotelLevels.BeginDataUpdate();
            this.gv_HotelLevels.EndDataUpdate();

            Contract.Customer customer = getCustomer();

            UIOperManager.SaveData(
                () =>
                {
                    return true;
                },
                () =>
                {
                    updateToCurCust(customer);
                    return _masterProxy.UpdateCustomer(CurCust).IsSuccess;
                },
                MsgResource.Save_Complete,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                );
        }

        private void setWebUser()
        {
            Frm_SetWebUser frm = new Frm_SetWebUser();

            frm.Load += (s, e) =>
                {
                    frm.CurWebU = CurCust.WebUser;
                };

            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
            {
                CurCust.WebUser = frm.CurWebU;
                this.txt_WebUser.SetText(CurCust.WebUser.Name);
            }
        }

        // current customer
        private Contract.Customer _curCust;
        public Contract.Customer CurCust
        {
            get { return _curCust; }
            set
            {
                _curCust = value;
                onCurCustChanged();
            }
        }

        private void onCurCustChanged()
        {
            if (CurCust == null)
            {
                // clear customer datas
                ControlExtensions.ClearValue(
                        this.txt_CAddress,
                        this.txt_CContactPerson1,
                        this.txt_CContactPerson2,
                        this.txt_CContactPersonPhone1,
                        this.txt_CContactPersonPhone11,
                        this.txt_CContactPersonPhone2,
                        this.txt_CContactPersonPhone22,
                        this.txt_CContactPersonTel1,
                        this.txt_CContactPersonTel2,
                        this.txt_CFax,
                        this.txt_CFinancialAffairs,
                        this.txt_CFinancialAffairsPhone1,
                        this.txt_CFinancialAffairsTel,
                        this.spe_Integral,
                        this.txt_CName,
                        this.txt_CNr,
                        this.txt_CRemark,
                        this.txt_WebUser,
                        this.spe_Amount
                    );

                this.bds_Passenger.DataSource = new List<Contract.Passenger>();
            }
            else
            {
                // set curstomer datas
                this.spe_Amount.SetValue(CurCust.Amount);
                this.txt_CAddress.SetText(CurCust.Address);
                this.txt_CContactPerson1.SetText(CurCust.ContactPerson1);
                this.txt_CContactPerson2.SetText(CurCust.ContactPerson2);
                this.txt_CContactPersonPhone1.SetText(CurCust.ContactPerson1Phone1);
                this.txt_CContactPersonPhone11.SetText(CurCust.ContactPerson1Phone2);
                this.txt_CContactPersonPhone2.SetText(CurCust.ContactPerson2Phone1);
                this.txt_CContactPersonPhone22.SetText(CurCust.ContactPerson2Phone2);
                this.txt_CContactPersonTel1.SetText(CurCust.ContactPerson1Tel);
                this.txt_CContactPersonTel2.SetText(CurCust.ContactPerson2Tel);
                this.txt_CFax.SetText(CurCust.Fax);
                this.txt_CFinancialAffairs.SetText(CurCust.FinancialAffairs);
                this.txt_CFinancialAffairsPhone1.SetText(CurCust.FinancialAffairsPhone1);
                this.txt_CFinancialAffairsPhone2.SetText(CurCust.FinancialAffairsPhone2);
                this.txt_CFinancialAffairsTel.SetText(CurCust.FinancialAffairsTel);
                this.spe_Integral.SetValue(CurCust.Intergral);
                this.txt_CName.SetText(CurCust.Name);
                this.txt_CNr.SetText(CurCust.Nr);
                if (CurCust.Nr == string.Empty)
                    this.txt_CNr.SetText("(保存时自动获取)");
                this.txt_CRemark.SetText(CurCust.Remark);
                this.cmb_Salesman.SetValue(CurCust.Salesman);

                this.bds_Passenger.DataSource = CurCust.PassengerList;

                this.spe_CFlightIntergralRate.SetValue(CurCust.FlightIntergralRate);

                if (CurCust.HotelIntergralList.Count == 0 && CurCust.HotelIntergralRate.HasValue)
                {
                    rad_Price.Checked = true;
                    spe_HotelIntergralRate.SetValue(CurCust.HotelIntergralRate.Value);
                }
                else if (CurCust.HotelIntergralList.Count > 0)
                {
                    rad_HotelLevel.Checked = true;
                    this.bds_CustomerHotelIntergralRate.DataSource = CurCust.HotelIntergralList;
                }

                if (CurCust.WebUser != null)
                    this.txt_WebUser.SetText(CurCust.WebUser.Name);
                else
                    this.txt_WebUser.SetText(string.Empty);
            }
        }


        private Contract.Customer getCustomer()
        {
            Contract.Customer customer = new Contract.Customer
            {
                Amount = spe_Amount.GetValue(),
                Address = txt_CAddress.GetText(),
                ContactPerson1 = txt_CContactPerson1.GetText(),
                ContactPerson1Phone1 = txt_CContactPersonPhone1.GetText(),
                ContactPerson1Phone2 = txt_CContactPersonPhone11.GetText(),
                ContactPerson1Tel = txt_CContactPersonTel1.GetText(),
                ContactPerson2 = txt_CContactPerson2.GetText(),
                ContactPerson2Phone1 = txt_CContactPersonPhone2.GetText(),
                ContactPerson2Phone2 = txt_CContactPersonPhone22.GetText(),
                ContactPerson2Tel = txt_CContactPersonTel2.GetText(),
                Fax = txt_CFax.GetText(),
                FinancialAffairs = txt_CFinancialAffairs.GetText(),
                FinancialAffairsPhone1 = txt_CFinancialAffairsPhone1.GetText(),
                FinancialAffairsPhone2 = txt_CFinancialAffairsPhone2.GetText(),
                FinancialAffairsTel = txt_CFinancialAffairsTel.GetText(),
                Name = txt_CName.GetText(),
                Nr = txt_CNr.GetText(),
                Remark = txt_CRemark.GetText(),
                Intergral = spe_Integral.GetValue(),
                FlightIntergralRate = spe_CFlightIntergralRate.GetValue(),
                Salesman = cmb_Salesman.GetValue<User>(),
                WebUser = CurCust.WebUser,
            };

            customer.PassengerList = this.bds_Passenger.DataSource as List<Passenger>;

            customer.HotelIntergralRate = rad_Price.Checked ? spe_HotelIntergralRate.GetValue(true) : null;

            customer.HotelIntergralList = rad_HotelLevel.Checked ? bds_CustomerHotelIntergralRate.DataSource as List<CustomerHotelIntergralRate> : new List<CustomerHotelIntergralRate>();

            return customer;
        }

        private void updateToCurCust(Contract.Customer customer)
        {
            CurCust.Amount = customer.Amount;
            CurCust.Address = customer.Address;
            CurCust.ContactPerson1 = customer.ContactPerson1;
            CurCust.ContactPerson1Phone1 = customer.ContactPerson1Phone1;
            CurCust.ContactPerson1Phone2 = customer.ContactPerson1Phone2;
            CurCust.ContactPerson1Tel = customer.ContactPerson1Tel;
            CurCust.ContactPerson2 = customer.ContactPerson2;
            CurCust.ContactPerson2Phone1 = customer.ContactPerson2Phone1;
            CurCust.ContactPerson2Phone2 = customer.ContactPerson2Phone2;
            CurCust.ContactPerson2Tel = customer.ContactPerson2Tel;
            CurCust.Fax = customer.Fax;
            CurCust.FinancialAffairs = customer.FinancialAffairs;
            CurCust.FinancialAffairsPhone1 = customer.FinancialAffairsPhone1;
            CurCust.FinancialAffairsPhone2 = customer.FinancialAffairsPhone2;
            CurCust.FinancialAffairsTel = customer.FinancialAffairsTel;
            CurCust.FlightIntergralRate = customer.FlightIntergralRate;
            CurCust.HotelIntergralList = customer.HotelIntergralList;
            CurCust.HotelIntergralRate = customer.HotelIntergralRate;
            CurCust.Intergral = customer.Intergral;
            CurCust.Name = customer.Name;
            CurCust.PassengerList = customer.PassengerList;
            CurCust.Remark = customer.Remark;
            CurCust.Salesman = customer.Salesman;
            CurCust.WebUser = customer.WebUser;

        }

        #region Passenger

        // selected passenger
        private Passenger _selPass;
        public Passenger SelPass
        {
            get { return _selPass; }
            set
            {
                _selPass = value;
                onSelPassChanged();
            }
        }

        private void onSelPassChanged()
        {
            if (SelPass == null)
            {
                // clear passenger datas
                ControlExtensions.ClearValue(
                        this.txt_PCardNr1,
                        this.txt_PCardNr2,
                        this.txt_PCardType1,
                        this.txt_PCardType2,
                        this.txt_PCName,
                        this.txt_PEName,
                        this.txt_PFightCard1,
                        this.txt_PFightCard2,
                        this.txt_PFightCard3,
                        this.txt_PNation,
                        this.txt_PSex,
                        this.txt_PTel1,
                        this.txt_PTel2,
                        this.dte_PBirthday,
                        this.txt_CardEffective1,
                        this.txt_CardEffective2
                    );
            }
            else
            {
                // set passenger datas
                this.txt_PCardNr1.SetText(SelPass.IDCardNr1);
                this.txt_PCardNr2.SetText(SelPass.IDCardNr2);
                this.txt_PCardType1.SetText(SelPass.IDCardType1);
                this.txt_PCardType2.SetText(SelPass.IDCardType2);
                this.txt_PCName.SetText(SelPass.CName);
                this.txt_PEName.SetText(SelPass.EName);
                this.txt_PFightCard1.SetText(SelPass.FlightCard1);
                this.txt_PFightCard2.SetText(SelPass.FlightCard2);
                this.txt_PFightCard3.SetText(SelPass.FlightCard3);
                this.txt_PNation.SetText(SelPass.Nation);
                this.txt_PSex.SetText(SelPass.Sex);
                this.txt_PTel1.SetText(SelPass.Tel1);
                this.txt_PTel2.SetText(SelPass.Tel2);
                this.dte_PBirthday.SetValue(SelPass.BirthDay);
                this.txt_CardEffective1.SetText(SelPass.IDCardEffective1);
                this.txt_CardEffective2.SetText(SelPass.IDCardEffective2);
            }
        }

        #endregion

        protected override void OnViewStateChanged()
        {
            if (ViewState == ViewStates.BROWSER)
            {
                spe_Amount.ReadOnly(true);
                txt_CName.ReadOnly(true);
                txt_CAddress.ReadOnly(true);
                txt_CFax.ReadOnly(true);
                cmb_Salesman.ReadOnly(true);
                spe_Integral.ReadOnly(true);
                txt_CRemark.ReadOnly(true);
                txt_CContactPerson1.ReadOnly(true);
                txt_CContactPerson2.ReadOnly(true);
                txt_CContactPersonTel1.ReadOnly(true);
                txt_CContactPersonTel2.ReadOnly(true);
                txt_CContactPersonPhone1.ReadOnly(true);
                txt_CContactPersonPhone2.ReadOnly(true);
                txt_CContactPersonPhone11.ReadOnly(true);
                txt_CContactPersonPhone22.ReadOnly(true);
                txt_CFinancialAffairs.ReadOnly(true);
                txt_CFinancialAffairsTel.ReadOnly(true);
                txt_CFinancialAffairsPhone1.ReadOnly(true);
                txt_CFinancialAffairsPhone2.ReadOnly(true);
                spe_CFlightIntergralRate.ReadOnly(true);
                spe_HotelIntergralRate.ReadOnly(true);
                rad_Price.ReadOnly(true);
                rad_HotelLevel.ReadOnly(true);

                gv_HotelLevels.OptionsBehavior.Editable = false;
                gv_HotelLevels.OptionsBehavior.ReadOnly = true;

                btn_SetWebUser.Visible = false;
                bbt_Save.Visibility = BarItemVisibility.Never;
                bbt_Save.Enabled = false;
                bbt_Add.Visibility = BarItemVisibility.Never;
                bbt_Add.Enabled = false;
                bbt_Edit.Visibility = BarItemVisibility.Never;
                bbt_Edit.Enabled = false;
            }
        }
    }
}
